﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Autodesk.Revit.DB;
using Autodesk.Revit.DB.Electrical;
using YArchitech.LIB;
using HYCodeBase.XmlUtility;

namespace HYElectricSetting
{
	public static class ElectricSettingDataAssist
	{
		public static List<RevitWireType> GetRevitWireTypes(Autodesk.Revit.DB.Document doc)
		{
			bool flag = doc == null;
			List<RevitWireType> result;
			if (flag)
			{
				result = null;
			}
			else
			{
				List<RevitWireType> list = new List<RevitWireType>();
				WireTypeSet wireTypes = doc.Settings.ElectricalSetting.WireTypes;
				WireTypeSetIterator wireTypeSetIterator = wireTypes.ForwardIterator();
				while (wireTypeSetIterator.MoveNext())
				{
					object obj = wireTypeSetIterator.Current;
					WireType wireType = obj as WireType;
					string name = wireType.Name;
					RevitWireType item2 = new RevitWireType(wireType.Name, wireType.Id);
					list.Add(item2);
				}

				list = (from n in list
				orderby n.Name descending
				select n).ToList<RevitWireType>();

				bool flag2 = ElectricSettingData.WireSettingData == null;
				if (flag2)
				{
					ElectricSettingData.ReadWireSettingData(doc);
				}
				using (List<RevitWireType>.Enumerator enumerator = list.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						RevitWireType item = enumerator.Current;
						bool flag3 = !ElectricSettingData.WireSettingData.Exists((WireSizeData wsd) => item.Name == wsd.WireType.Name);
						if (!flag3)
						{
							WireSizeData wireSizeData = ElectricSettingData.WireSettingData.Find((WireSizeData wsd) => item.Name == wsd.WireType.Name);
							foreach (KeyValuePair<double, double> keyValuePair in wireSizeData.WireSizes)
							{
								item.WireSizeSet.Add(keyValuePair.Key, keyValuePair.Value);
							}


							var list2 = (from n in item.WireSizeSet.Keys
							orderby n descending
							select n);

                            //foreach (var da in list2)
                            //{
                            //    item.WireSizeSet.Add(da);
                            //}


						}
					}
				}
				result = list;
			}
			return result;
		}

		public static List<RevitConduitType> GetRevitConduitTypes(Autodesk.Revit.DB.Document doc)
		{
			bool flag = doc == null;
			List<RevitConduitType> result;
			if (flag)
			{
				result = null;
			}
			else
			{
				List<RevitConduitType> list = new List<RevitConduitType>();
				FilteredElementCollector filteredElementCollector = new FilteredElementCollector(doc);
				ICollection<ElementId> collection = filteredElementCollector.OfClass(typeof(ConduitType)).ToElementIds();
				foreach (ElementId elementId in collection)
				{
					ConduitType conduitType = doc.GetElement(elementId) as ConduitType;
					bool flag2 = conduitType == null || !conduitType.IsWithFitting;
					if (!flag2)
					{
						RevitConduitType item2 = new RevitConduitType(conduitType.Name, elementId);
						list.Add(item2);
					}
				}
				list = (from n in list
				orderby n.Name descending
                        select n).ToList<RevitConduitType>();
				bool flag3 = ElectricSettingData.ConduitSettingData == null;
				if (flag3)
				{
					ElectricSettingData.ReadConduitSettingData(doc);
				}
				using (List<RevitConduitType>.Enumerator enumerator2 = list.GetEnumerator())
				{
					while (enumerator2.MoveNext())
					{
						RevitConduitType item = enumerator2.Current;
						bool flag4 = !ElectricSettingData.ConduitSettingData.Exists((ConduitTypeData ctd) => item.Name == ctd.Name);
						if (!flag4)
						{
							ConduitTypeData conduitTypeData = ElectricSettingData.ConduitSettingData.Find((ConduitTypeData ctd) => item.Name == ctd.Name);
							item.ConduitSizeSet = conduitTypeData.SizeSet.ToList<int>();
							item.ConduitSizeSet.Sort();
						}
					}
				}
				result = list;
			}
			return result;
		}

		public static List<RevitConduitType> GetConduitTypesForWire(Autodesk.Revit.DB.Document doc, string wireTypeName)
		{
			List<RevitConduitType> list = new List<RevitConduitType>();
			bool flag = doc == null || string.IsNullOrEmpty(wireTypeName);
			List<RevitConduitType> result;
			if (flag)
			{
				result = list;
			}
			else
			{
				List<RevitConduitType> revitConduitTypes = ElectricSettingDataAssist.GetRevitConduitTypes(doc);
				bool flag2 = ElectricSettingData.WireConduitSettingData == null;
				if (flag2)
				{
					ElectricSettingData.ReadWireConduitSettingData();
				}
				using (List<WireConduitData>.Enumerator enumerator = ElectricSettingData.WireConduitSettingData.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						WireConduitData item = enumerator.Current;
						bool flag3 = wireTypeName != item.WireTypeName;
						if (!flag3)
						{
							bool flag4 = !revitConduitTypes.Exists((RevitConduitType rct) => rct.Name == item.ConduitTypeName);
							if (!flag4)
							{
								RevitConduitType item2 = revitConduitTypes.Find((RevitConduitType rct) => rct.Name == item.ConduitTypeName);
								list.Add(item2);
							}
						}
					}
				}
				result = list;
			}
			return result;
		}

		public static int GetConduitDiameter(Autodesk.Revit.DB.Document doc, string wireTypeName, double section, string conduitTypeName)
		{
			bool flag = ElectricSettingData.ConduitSettingData == null;
			if (flag)
			{
				ElectricSettingData.ReadConduitSettingData(doc);
			}
			bool flag2 = ElectricSettingData.WireConduitSettingData == null;
			if (flag2)
			{
				ElectricSettingData.ReadWireConduitSettingData();
			}
			bool flag3 = !ElectricSettingData.ConduitSettingData.Exists((ConduitTypeData ctd) => conduitTypeName == ctd.Name);
			int result;
			if (flag3)
			{
				result = 0;
			}
			else
			{
				ConduitTypeData conduitTypeData = ElectricSettingData.ConduitSettingData.Find((ConduitTypeData ctd) => conduitTypeName == ctd.Name);
				bool flag4 = conduitTypeData.SizeSet.Count <= 0;
				if (flag4)
				{
					result = 0;
				}
				else
				{
					bool flag5 = ElectricSettingData.WireConduitSettingData.Exists((WireConduitData wcd) => wireTypeName == wcd.WireTypeName && conduitTypeName == wcd.ConduitTypeName);
					if (flag5)
					{
						WireConduitData wireConduitData = ElectricSettingData.WireConduitSettingData.Find((WireConduitData wcd) => wireTypeName == wcd.WireTypeName && conduitTypeName == wcd.ConduitTypeName);
						bool flag6 = wireConduitData.WireConduitSet.ContainsKey(section);
						if (flag6)
						{
							int num = wireConduitData.WireConduitSet[section];
							bool flag7 = conduitTypeData.SizeSet.Contains(num);
							if (flag7)
							{
								return num;
							}
						}
					}
					result = conduitTypeData.SizeSet.Min();
				}
			}
			return result;
		}

		public static int GetLightningSupportCardSpacing(int defaultValue = 1000)
		{
			string xmlPath = Path.Combine(Product.UserDataLocation, "Lightning");
			HYCodeBase.XmlUtility.XmlHelper xmlHelper = new HYCodeBase.XmlUtility.XmlHelper(xmlPath, "ElectricData");
			xmlHelper.SelectNode("LGing");
			return Convert.ToInt32(xmlHelper.GetAttributeEx("LSpacing", defaultValue.ToString()));
		}

		public static int GetGroundingPoleSpacing(int defaultValue = 5000)
		{
			string xmlPath = Path.Combine(Product.UserDataLocation, "Lightning");
			HYCodeBase.XmlUtility.XmlHelper xmlHelper = new HYCodeBase.XmlUtility.XmlHelper(xmlPath, "ElectricData");
			xmlHelper.SelectNode("LGing");
			return Convert.ToInt32(xmlHelper.GetAttributeEx("GSpacing", defaultValue.ToString()));
		}
	}
}
